package com.zp.base.admin.interceptor;

import com.zp.base.admin.entity.MenuEntity;
import com.zp.base.admin.entity.UserEntity;
import com.zp.base.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取用户资权限页面展示
 * @author ZhangPeng
 * @email zhangpeng98@aliyun.com
 * @create 2021-03-01 10:40
 */
@Component
public class MenuInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断是否是ajax请求
        String header = request.getHeader("X-Requested-With");
        if (!"XMLHttpRequest".equals(header)) {
            //不是ajax请求
            //获取登录用户的权限
            UserEntity principal = null;
            if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
                principal = (UserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            }
            List<MenuEntity> menuEntityList = userService.getMenusByUserId(principal.getId());
            //获取一级菜单和二级菜单
            List<MenuEntity> topMenus = null;
            List<MenuEntity> secondMenus = null;
            List<MenuEntity> threeMenus = null;
            if (menuEntityList != null && menuEntityList.size() > 0) {
                topMenus = menuEntityList.stream().filter(menuEntity -> menuEntity.getParentId() == null).collect(Collectors.toList());
                if (topMenus != null && topMenus.size() > 0) {
                    secondMenus = getSecondMenus(topMenus, menuEntityList);
                }
                String requestURI = request.getRequestURI();
                if (secondMenus != null && secondMenus.size() > 0) {
                    List<MenuEntity> collect = secondMenus.stream().filter(menuEntity -> menuEntity.getUrl().equals(requestURI)).collect(Collectors.toList());
                    if (collect != null && collect.size() > 0) {
                        threeMenus = getThreedMenus(collect.get(0), menuEntityList);
                    }
                }
                request.setAttribute("userTopMenus", topMenus);
                request.setAttribute("userSecondMenus", secondMenus);
                request.setAttribute("userThirdMenus", threeMenus);
            }
        }
        return true;
    }

    private List<MenuEntity> getThreedMenus(MenuEntity menuEntity, List<MenuEntity> menuEntityList) {
        return menuEntityList.stream().filter(menuEntity1 -> menuEntity.getId().equals(menuEntity1.getParentId())).collect(Collectors.toList());
}

    private List<MenuEntity> getSecondMenus(List<MenuEntity> topMenus, List<MenuEntity> menuEntityList) {
        return menuEntityList.stream().filter(menuEntity -> find(menuEntity.getParentId(), topMenus)).collect(Collectors.toList());
    }

    private boolean find(Long parentId, List<MenuEntity> topMenus) {
        for (MenuEntity topMenu : topMenus) {
            if (topMenu.getId().equals(parentId)) {
                return true;
            }
        }
        return false;
    }

}
